const express = require('express');
const public = express.Router();
const User = require('../model/user')
//注册用户
public.post('/register', (req, res) => {
  User.create(req.body)
    .then(doc => console.log(doc))
    .catch(err => console.log(err))
  res.send('ok')
})
//登录页面请求
public.post('/login', (req, res) => {
  const { username, password } = req.body;
  User.findOne({ username: username, password: password }).then(result => {
    if (result) {
      res.send({
        code: 200, data: result
      })
    } else {
      res.send({
        code: 401
      })
    }
  });
})
//我的页面用户信息更新
public.post('/update', (req, res) => {
  User.findOne({ _id: req.query.id }).then(result => {
    if (result) {
      res.send({
        code: 200, data: result
      })
    } else {
      res.send({
        code: 401
      })
    }
  });
})
module.exports = public;